//
//  HKClassUtils.m
//  HKOnLine
//
//  Created by 徐小雷 on 16/8/23.
//  Copyright © 2016年 xuxiaolei. All rights reserved.
//

#import "HKClassUtils.h"
#import <objc/runtime.h>

@implementation UIView (lsy_utilView)

///<--------------快速坐标---------------->
- (void)setX_:(CGFloat)x
{
    CGRect rect = self.frame;
    rect.origin.x = x;
    self.frame = rect;
}

- (CGFloat)getX_
{
    return self.frame.origin.x;
}

- (void)setY_:(CGFloat)y
{
    CGRect rect = self.frame;
    rect.origin.y = y;
    self.frame = rect;
}

- (CGFloat)getY_
{
    return self.frame.origin.y;
}

- (void)setWidth_:(CGFloat)width
{
    CGRect rect = self.frame;
    rect.size.width = width;
    self.frame = rect;
}

- (CGFloat)getWidth_
{
    return self.frame.size.width;
}

- (void)setHeight_:(CGFloat)height
{
    CGRect rect = self.frame;
    rect.size.height = height;
    self.frame = rect;
}

- (CGFloat)getHeight_
{
    return self.frame.size.height;
}

- (void)setSize_:(CGSize)size
{
    CGRect rect = self.frame;
    rect.size = size;
    self.frame = rect;
}

- (CGSize)getSize_
{
    return self.frame.size;
}

- (void)setOrigin_:(CGPoint)origin
{
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGPoint)getOrigin_
{
    return self.frame.origin;
}

- (void)setRightPoint_:(CGPoint)rightPoint{
    CGRect frame = self.frame;
    frame.origin.x = rightPoint.x - frame.size.width;
    frame.origin.y = rightPoint.y;
    self.frame = frame;
}

- (CGPoint)getRightPoint_
{
    return CGPointMake(self.x + self.width, self.y);
}

- (CGPoint)boundsCenter{
    return CGPointMake(self.width / 2, self.height/2);
}

- (void)setTop_:(CGFloat)top
{
    self.y = top;
}

- (CGFloat)getTop_
{
    return self.y;
}

- (void)setLeft_:(CGFloat)left
{
    self.x = left;
}

- (CGFloat)getLeft_
{
    return self.x;
}

- (void)setBottom_:(CGFloat)bottom
{
    self.y = bottom - self.bottom;
}

- (CGFloat)getBottom_
{
    return self.y + self.height;
}

- (void)setRight_:(CGFloat)right
{
    self.x = right - self.width;
}

- (CGFloat)getRight_
{
    return self.x + self.width;
}

- (void)setCenterX_:(CGFloat)centerX
{
    [self setCenter:(CGPoint){centerX, self.center.y}];
}

- (CGFloat)getCenterX_
{
    return self.center.x;
}

- (void)setCenterY_:(CGFloat)centerY
{
    [self setCenter:(CGPoint){self.center.x, centerY}];
}

- (CGFloat)getCenterY_
{
    return self.center.y;
}

- (void)lsy_setFrame:(CGFloat)x y:(CGFloat)y w:(CGFloat)w h:(CGFloat)h
{
    [self setFrame:CGRectMake(x, y, w, h)];
}

- (void)lsy_setXY:(CGFloat)x Y:(CGFloat)y
{
    CGPoint point;
    point.x = x;
    point.y = y;
    
    self.origin = point;
}

- (void)lsy_setSizew:(CGFloat)w h:(CGFloat)h
{
    CGSize size;
    size.width = w;
    size.height = h;
    
    self.size = size;
}

- (CGRect)getFrameApplyAffineTransform_
{
    return CGRectApplyAffineTransform(self.frame, self.transform);
}

- (CGRect)getBoundsApplyAffineTransform_
{
    return CGRectApplyAffineTransform(self.bounds, self.transform);
}

///<--------------快速坐标 End---------------->

///<--------------快速排版---------------->

//添加子视图@[]
- (void)lsy_addSubviewAry:(NSArray *)objects
{
    for(UIView *vi in objects)
        [self addSubview:vi];
}

//置父视图顶部
- (void)lsy_setSuperViewTop
{
    self.y = 0;
}

//置父视图左边
- (void)lsy_setSuperViewLeft
{
    self.x = 0;
}

//置父视图底部
- (void)lsy_setSuperViewBotm
{
    NSAssert(self.superview, @"not superView!");
    self.y = self.superview.height - self.height;
}

//置父视图右边
- (void)lsy_setSuperViewRight
{
    NSAssert(self.superview, @"not superView!");
    self.x = self.superview.width - self.width;
}

//置View到指定View的顶部，并且设置间距
- (void)lsy_setTopFromView:(UIView*)vi dis:(CGFloat)dis
{
    self.y = vi.y - dis - self.height;
}

//置View到指定View左边，并且设置间距
- (void)lsy_setLeftFromView:(UIView*)vi dis:(CGFloat)dis
{
    self.x = vi.x - dis - self.width;
}

//置View到指定View底部，并且设置间距
- (void)lsy_setBotmFromView:(UIView*)vi dis:(CGFloat)dis
{
    self.y = vi.bottom + dis;
}

//置View到指定View右边，并且设置间距
- (void)lsy_setRightFromView:(UIView*)vi dis:(CGFloat)dis
{
    self.x = vi.right + dis;
}

//置View到指定父视图的顶部，并且设置间距
- (void)lsy_setTopFromSuperViewWithDis:(CGFloat)dis
{
    NSAssert(self.superview, @"not superView!");
    self.y = dis;
}

//置View到指定父视图左边，并且设置间距
- (void)lsy_setLeftFromSuperViewWithDis:(CGFloat)dis
{
    NSAssert(self.superview, @"not superView!");
    self.x = dis;
}

//置View到指定父视图底部，并且设置间距
- (void)lsy_setBotmFromSuperViewWithDis:(CGFloat)dis
{
    NSAssert(self.superview, @"not superView!");
    self.y = self.superview.height - self.height - dis;
}

//置View到指定父视图右边，并且设置间距
- (void)lsy_setRightFromSuperViewWithDis:(CGFloat)dis
{
    NSAssert(self.superview, @"not superView!");
    self.x = self.superview.width - self.width - dis;
}

//使用Margin设置坐标-相对父视图
- (void)lsy_setMarginTop:(CGFloat)top left:(CGFloat)left bottom:(CGFloat)bottom right:(CGFloat)right
{
    NSAssert(self.superview, @"没有父视图!");
    CGRect frame;
    frame.origin.y = top;
    frame.origin.x = left;
    frame.size.height = self.superview.height - frame.origin.y - bottom;
    frame.size.width = self.superview.width - frame.origin.x - right;
    self.frame = frame;
}

//设置UIViewAutoresizingFlexibleLeftMargin
- (void)lsy_setLeftMargin
{
    self.autoresizingMask |= UIViewAutoresizingFlexibleLeftMargin;
}

//设置UIViewAutoresizingFlexibleRightMargin
- (void)lsy_setRightMargin
{
    self.autoresizingMask |= UIViewAutoresizingFlexibleRightMargin;
}

//设置UIViewAutoresizingFlexibleTopMargin
- (void)lsy_setTopMargin
{
    self.autoresizingMask |= UIViewAutoresizingFlexibleTopMargin;
}

//设置UIViewAutoresizingFlexibleWidth
- (void)lsy_setWidthMargin
{
    self.autoresizingMask |= UIViewAutoresizingFlexibleWidth;
}

//设置UIViewAutoresizingFlexibleHeight
- (void)lsy_setHeightMargin
{
    self.autoresizingMask |= UIViewAutoresizingFlexibleHeight;
}

//设置UIViewAutoresizingFlexibleBottomMargin
- (void)lsy_setBottomMargin
{
    self.autoresizingMask |= UIViewAutoresizingFlexibleBottomMargin;
}

//设置AllMargin
- (void)lsy_setAllMargin
{
    [self lsy_setLeftMargin];
    [self lsy_setRightMargin];
    [self lsy_setTopMargin];
    [self lsy_setHeightMargin];
    [self lsy_setBottomMargin];
}

//输出View的Frame
- (void)lsy_ptrFrame
{
    NSLog(@"[%@]", NSStringFromCGRect(self.frame));
}

///<--------------快速排版 End---------------->

///<--------------杂项---------------->

//使用空白背景颜色
- (void)lsy_setBgrClearColor
{
    self.backgroundColor = [UIColor clearColor];
}

- (CAGradientLayer *)lsy_gradientWithColors:(NSArray *)colors locations:(NSArray *)locations
{
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.bounds;
    gradient.colors = colors;
    if (locations) {
        gradient.locations = locations;
    }
    [self.layer insertSublayer:gradient atIndex:0];
    
    return gradient;
}

///<--------------杂项 End---------------->

@end

@implementation NSString (lsy_utilNSString)

//iPhone6某些机子无法转换
- (double)doubleValue
{
    return atof(self.UTF8String);
}

//iPhone6某些机子无法转换
- (NSInteger)integerValue
{
    return atol(self.UTF8String);
}

@end

@implementation NSTimer (lsy_utilNSTimer)

- (void)lsy_addRunLoop
{
    [[NSRunLoop currentRunLoop] addTimer:self forMode:NSDefaultRunLoopMode];
}

@end

